package com.shd.edanpei.presenter;

import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.exception.ResponeThrowable;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.bean.PointImage;
import com.shd.edanpei.bean.ScoreModel;
import com.shd.edanpei.bean.PagingModel;
import com.shd.edanpei.presenter.contract.AccountContract;

import io.reactivex.android.schedulers.AndroidSchedulers;


public class AccountPresenter extends BasePresenterImpl<AccountContract.AccountIView>
        implements AccountContract.AccountIPresenter {

    private int currentPage;
    private int totalPage;

    public  AccountPresenter(AccountContract.AccountIView mView) {
        super(mView);
    }

    @Override
    public void getAccountInfo() {
        mBaseIView.showLoading("");
        ApiCreator.getApi().integeral()
                .compose(RxTransformer.<ScoreModel>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ScoreModel>() {
                    @Override
                    protected void onSuccess(ScoreModel data) {
                        mBaseIView.setAccountInfo(data);
                    }

                    @Override
                    public void onFinish() {
                        mBaseIView.stopLoading();
                    }
                });
    }

    @Override
    public void refresh(final String type) {
        currentPage = 1;
        ApiCreator.getApi().accountList(type, currentPage)
                .compose(RxTransformer.<PagingModel<ScoreModel>>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<PagingModel<ScoreModel>>() {

                    protected void onSuccess(PagingModel<ScoreModel> data) {
                        if (data != null && data.getList().size() > 0) {
                            totalPage = data.getTotalPage();
                            mBaseIView.getAdapter().setNewData(data.getList());
                            if (currentPage >= totalPage) {
                                mBaseIView.getAdapter().loadMoreEnd();
                            } else {
                                mBaseIView.getAdapter().loadMoreComplete();
                            }
                            currentPage++;
                        } else {
                            mBaseIView.getAdapter().setNewData(null);
//                            mBaseIView.getAdapter().setEmptyView(getNullLayout());
                        }
                    }


                    @Override
                    public void onFailure(ResponeThrowable throwable) {
//                        mBaseIView.getAdapter().setEmptyView(getNullLayout());

                    }

                });
    }

    @Override
    public void loadMore(final String type) {
        ApiCreator.getApi().accountList(type, currentPage)
                .compose(RxTransformer.<PagingModel<ScoreModel>>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<PagingModel<ScoreModel>>() {
                    @Override
                    protected void onSuccess(PagingModel<ScoreModel> data) {
                        if (data != null && data.getList().size() > 0) {
                            mBaseIView.getAdapter().addData(data.getList());
                            if (currentPage >= totalPage) {
                                mBaseIView.getAdapter().loadMoreEnd();
                            } else {
                                mBaseIView.getAdapter().loadMoreComplete();
                            }
                            currentPage++;
                        } else {
                            mBaseIView.getAdapter().loadMoreEnd();
                        }
                    }

                    @Override
                    public void onFailure(ResponeThrowable throwable) {
                        mBaseIView.getAdapter().loadMoreFail();
                    }
                });
    }

    @Override
    public void getImage() {
        ApiCreator.getApi().pointsImage()
                .compose(RxTransformer.<PointImage>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<PointImage>() {
                    @Override
                    protected void onSuccess(PointImage data) {
                        mBaseIView.setImage(data.getValues());
                    }
                });
    }

//    private View getNullLayout() {
//        View view = LayoutInflater.from(mBaseIView.getContext()).inflate(R.layout.null_layout, null);
//        ImageView ivPic = view.findViewById(R.id.iv_null_layout_pic);
//        TextView tvHint = view.findViewById(R.id.tv_null_layout_hint);
//        ivPic.setImageResource(R.drawable.pic_none_score);
//        tvHint.setText("暂无相关明细");
//        return view;
//    }
}
